<?php
namespace app\api\controller;

use app\admin\model\Airport;
use app\admin\model\Business;
use app\admin\model\Region;
use app\admin\model\SelectedArea;
use app\admin\model\SelectedCollection;
use app\admin\model\SelectedPeople;
use app\admin\model\Subway;
use think\Controller;
use app\admin\model\ListContent;
use think\Request;

class Selected extends Controller
{
    public function selectedList(Request $request){
        $res = [];
        $data = $request->post();
        $page = $request->get();
        $selected = new ListContent();
        if(!isset($data['type']) or !isset($data['city']) or !isset($page['page']) ){
            $res['status'] = 400;
            echo json_encode($res);die;
        }
        if($data['type'] == 1){
            $data = $request->post();
            //永远存在定位城市
            $where['city_id'] = $data['city'];
            //开启状态
            $where['status'] = 1;
            //大于最小面积
            if(isset($data['area']))$where['area_min'] = ['gt',$data['area']];
            //大于最小人数
            if(isset($data['people']))$where['number_min'] = ['gt',$data['people']];
            //行政区
            if(isset($data['region']))$where['region'] = $data['region'];
            //地铁
            if(isset($data['subway']))$where['subway'] = $data['subway'];
            //机场火车
            if(isset($data['airport']))$where['airport'] = $data['airport'];
            //商业区
            if(isset($data['business']))$where['business'] = $data['business'];
            $rs = $selected->getAllPage($where);
            if($rs){
                $res['data']['code'] = 1;
                $res['data']['list'] = $rs;
            }else{
                $res['code'] = 0;
            }
        }else{
            //永远存在定位城市
            $where['city_id'] = $data['city'];
            //开启状态
            $where['status'] = 1;
            $where['title'] = ['like',"%".$data['selected'].'%'];
            $rs = $selected->getAll($where);
            if($rs){
                $res['data']['code'] = 1;
                $res['data']['list'] = $rs;
            }else{
                $res['data']['code'] = 0;
            }
        }
        $res['status'] = 200;
        echo json_encode($res);
    }

    public function selectedDetails(Request $request){
        $data = $request->post();
        $res = [];
        if(!isset($data['id']) ){
            $res['status'] = 400;
            echo json_encode($res);die;
        }
        $selected = new ListContent();
        $where['id'] = $data['id'];
        $list = $selected->getOne($where);
        if($list == null){
            $res['data']['code'] = 0;
            $res['data']['msg'] = '获取失败';
        }else{
            //查询是否已添加购物车
            $shopCart = new \app\admin\model\ShopCart();
            $shopWhere['user_id'] = isset($data['user_id']) ? $data['user_id'] : 0;
            $shopWhere['pr_type'] = 0;
            $shopWhere['pr_id'] = $data['id'];
            if($shopCart->getOne($shopWhere)){
                $list['in_cart'] = 1;
            }else{
                $list['in_cart'] = '';
            }

            //查询是否已添加收藏
            $collection = new \app\admin\model\Collection();
            $collectionWhere['user_id'] = isset($data['user_id']) ? $data['user_id'] : 0;
            $collectionWhere['p_id'] = $data['id'];
            $collectionWhere['type'] = 'selected';

            if($collection->getOne($collectionWhere)){
                $list['in_collection'] = 1;
            }else{
                $list['in_collection'] = '';
            }

            $res['data']['code'] = 1;
            $res['data']['msg'] = '获取成功';
            $res['data']['list'] = $list;
        }
        $res['status'] = 200;
        echo json_encode($res);
    }

    public function selectedOption(Request $request){
        $res = [];
        $data = $request->post();

        $airport = new Airport();
        $business = new Business();
        $region = new Region();
        $subway = new Subway();
        $selectedArea = new SelectedArea();
        $selectedPeople = new SelectedPeople();

        $where['id']=['neq',0];
        $airportList = $airport->getAll($where);
        $businessList = $business->getAll($where);
        $regionList = $region->getAll($where);
        $subwayList = $subway->getAll($where);
        $selectedAreaList = $selectedArea->getAll($where);
        $selectedPeopleList = $selectedPeople->getAll($where);

        if(!isset($data['selectedOption'])){
            $res['status'] = 400;
            echo json_encode($res);die;
        }
        if($airportList == null or $businessList == null or $regionList == null or $subwayList == null or $selectedAreaList == null or $selectedPeopleList == null){
            $res['data']['code'] = 0;
            $res['data']['msg'] = '获取失败';
        }else{
            $res['data']['code'] = 1;
            $res['data']['msg'] = '获取成功';
            $res['data']['list']['airportList']  = $airportList;
            $res['data']['list']['businessList'] = $businessList;
            $res['data']['list']['regionList']   = $regionList;
            $res['data']['list']['subwayList']   = $subwayList;
            $res['data']['list']['selectedAreaList']   = $selectedAreaList;
            $res['data']['list']['selectedPeopleList']   = $selectedPeopleList;
        }
        $res['status'] = 200;
        echo json_encode($res);
    }

    public function selectedCollectionChange(Request $request){
        $res = [];
        $data = $request->post();

        $selectedCollection = new SelectedCollection();
        if($data['type'] == 1){
            if(!isset($data['type']) or !isset($data['user_id']) or !isset($data['s_id']) or !isset($data['s_name']) or !isset($data['s_image'])){
                $res['status'] = 400;
                echo json_encode($res);die;
            }

            //查询是否收藏过
            $where['user_id'] = $data['user_id'];
            $where['s_id'] = $data['s_id'];
            $isCollection = $selectedCollection->getOne($where);
            if($isCollection){
                $res['data']['code'] = 0;
                $res['data']['msg'] = '请勿重复收藏';
                $res['status'] = 200;
                echo json_encode($res);die;
            }
            //添加收藏
            $add['user_id'] = $data['user_id'];
            $add['s_id'] = $data['s_id'];
            $add['s_name'] = $data['s_name'];
            $add['s_image'] = $data['s_image'];
            $add['c_time'] = time();

            $rs = $selectedCollection->add($add);
            if($rs){
                $res['data']['code'] = 1;
                $res['data']['msg'] = '收藏成功';
            }else{
                $res['data']['code'] = 0;
                $res['data']['msg'] = '收藏失败';
            }
        }else if($data['type'] == 0){
            if(!isset($data['type']) or !isset($data['user_id']) or !isset($data['s_id'])){
                $res['status'] = 400;
                echo json_encode($res);die;
            }

            //删除收藏
            $where['user_id'] = $data['user_id'];
            $where['s_id'] = $data['s_id'];

            $rs = $selectedCollection->del($where);
            if($rs){
                $res['data']['code'] = 1;
                $res['data']['msg'] = '已取消收藏';
            }else{
                $res['data']['code'] = 0;
                $res['data']['msg'] = '取消收藏失败';
            }
        }else{
            $res['data']['code'] = 0;
            $res['data']['msg'] = '操作失败';
        }

        $res['status'] = 200;
        echo json_encode($res);
    }
}